package com.atom.common.remote.user;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.impl.util.json.JSONArray;
import org.activiti.engine.impl.util.json.JSONException;
import org.activiti.engine.impl.util.json.JSONObject;

import com.atom.common.exception.AvensNullPointerException;
import com.atom.common.util.JsonResultParser;
import com.atom.common.util.PreConditionUtil;

public abstract class AbstractUserResolver {
  
  @Resource protected RemoteUserDao remoteUserDao;
  
  /**
   * Description: 构建用户信息
   * @param userId
   * @return
   */
  public UserUtil createUserUtil(String userId) {
    String userInfo = remoteUserDao.queryUserInfo("userid", userId);
    JSONArray array = new JSONArray(userInfo);
    if (array == null || array.length() == 0) {
      return null;
    }
    JSONObject userJSON = array.getJSONObject(0);
    try {
      UserUtil user = new UserUtil();
      user.setOrgid(userJSON.getString("orgid"));
      user.setOrgname(userJSON.getString("orgname"));
      user.setUserid(userJSON.getString("userid"));
      user.setUsername(userJSON.getString("username"));
      return user;
    } catch (JSONException e) {
      return null;
    }
  }
  
  /**
   * Description: 根据用户id查找用户名
   * @param userId
   * @return
   */
  public String getUserNameByUserId(String userId) {
    String userInfo = remoteUserDao.queryUserInfo("userid", userId);
    JSONArray array = new JSONArray(userInfo);
    if (array == null || array.length() == 0) {
      return null;
    }
    JSONObject userJSON = array.getJSONObject(0);
    try {
      return userJSON.getString("username");
    } catch (JSONException e) {
      return null;
    }
  }
  

  /**
   * Description: 获取部门正主任
   * @param orgid
   * @return
   */
  public UserUtil getZhengZhuRen(String orgid) {
//    String userInfo = remoteUserDao.oaQuery(orgid, "主任");
    String userInfo = "[{\"userid\"=\"userid1\",\"username\"=\"username1\",\"orgid\"=\"orgid\",\"orgname\"=\"orgname\",\"position\"=\"position\"}]";
    JSONArray array = new JSONArray(userInfo);
    if (array == null || array.length() == 0) {
      return null;
    }
    JSONObject userJSON = array.getJSONObject(0);
    UserUtil user = new UserUtil(userJSON);
    return user;
  }

  /**
   * Description: 获取正馆长
   * @return
   */
  public UserUtil getZhengGuanZhang() {
    String userInfo = remoteUserDao.queryUserInfo("positionid", "1");
    JSONArray array = new JSONArray(userInfo);
    if (array == null || array.length() == 0) {
      return null;
    }
    JSONObject userJSON = array.getJSONObject(0);
    UserUtil user = new UserUtil(userJSON);
    return user;
  } 

  /**
   * Description: 获取总账管理员
   * @return
   */
  public List<UserUtil> getZongZhangGuanLiYuan() {
    String zzgly = remoteUserDao.getUserByRole("zongzhangguanliyuan");
    List<UserUtil> users = new ArrayList<UserUtil>();
    String data = JsonResultParser.getData(zzgly);
    JSONArray array = new JSONArray(data);
    for (int i=0; i<array.length(); i++) {
      UserUtil user = new UserUtil(array.getJSONObject(i));
      users.add(user);
    }
    return users;
  }

  /**
   * Description: 获取所有馆领导
   * @return
   */
  public List<UserUtil> getGuanZhang() {
    String userInfo = remoteUserDao.listUserByOrgid("guanlingdao");
    JSONArray array = new JSONArray(userInfo);
    if (array == null || array.length() == 0) {
      return null;
    }
    List<UserUtil> userList = new ArrayList<UserUtil>();
    for (int i=0; i<array.length(); i++) {
      JSONObject userJSON = array.getJSONObject(0);
      UserUtil user = new UserUtil(userJSON);
      userList.add(user);
    }
    return userList;
  }
  
  public String getIds(List<UserUtil> users) {
    String nextAssignees = "";
    for (UserUtil user : users) {
      nextAssignees = nextAssignees + user.getUserid() + ",";
    }
    nextAssignees.substring(0, nextAssignees.length() - 1);
    return nextAssignees;
  }

}
